﻿var widgetsManager = {} || widgetsManager;
widgetsManager = {
    initialize: function (pageId, sortableHolder, sortableHandle, sortablePlaceHolderClass, refreshButton, draggableWidgetItem, newWidgetSnapFunc) {
        //Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(
        (function (sender, e) {
            var _fromObj = null;
            var _toObj = null;
            if (!$(sortableHandle).hasClass("ui-sortable")) {
                //Initialize sortable
                $(sortableHolder).sortable({
                    handle: sortableHandle,
                    connectWith: sortableHolder,
                    placeholder: sortablePlaceHolderClass,
                    tolerance: "pointer",
                    cursor: "move",
                    items: ".sortable:not(.locked)",
                    opacity: 0.5,
                    start: function (event, ui) {
                        $(ui.placeholder).height($(ui.item).height());
                        var columnObj = $(ui.item).parents("[columnid]");
                        var widgetInfoObj = ($(ui.item).filter("[widgettypeid]").length > 0)
                                                                ? $(ui.item).filter("[widgettypeid]")
                                                                : $(ui.item).find("[widgettypeid]");
                        _fromObj = {
                            widgettypeid: $(widgetInfoObj).attr("widgettypeid"),
                            widgetid: $(widgetInfoObj).attr("widgetid"),
                            columnid: columnObj.attr("columnid"),
                            index: columnObj.find("[widgettypeid]").index(widgetInfoObj)
                        };
                    },
                    update: function (event, ui) {
                        if (_fromObj != null) {
                            var columnObj = $(ui.item).parents("[columnid]");
                            var widgetInfoObj = ($(ui.item).filter("[widgettypeid]").length > 0)
                                                                ? $(ui.item).filter("[widgettypeid]")
                                                                : $(ui.item).find("[widgettypeid]");
                            _toObj = {
                                widgettypeid: $(widgetInfoObj).attr("widgettypeid"),
                                widgetid: $(widgetInfoObj).attr("widgetid"),
                                columnid: columnObj.attr("columnid"),
                                index: columnObj.find("[widgettypeid]").index(widgetInfoObj)
                            };
                            //coverPage("refresh widgets... ...");
                            var columnContainerObj = $(ui.item).parents(sortableHolder);
                            if (_fromObj.widgettypeid == _toObj.widgettypeid
                                                && _fromObj.index == _toObj.index
                                                && _fromObj.columnid == _toObj.columnid) {
                                widgetsManager.addWidget(_fromObj, function () {
                                    eval($(refreshButton).attr("href"));
                                });
                                var newWidgetTitle = $(ui.item).find(".newwidgetitemtitle").text();
                                newWidgetSnapFunc = newWidgetSnapFunc || widgetsManager.createNewWidgetSnap;
                                $(ui.item).replaceWith(newWidgetSnapFunc(newWidgetTitle));
                            }
                            else {
                                widgetsManager.moveWidget(_fromObj, _toObj, function () {
                                    var result = Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack();
                                    while (result)
                                        result = Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack();

                                    eval($(refreshButton).attr("href"));
                                });
                            }
                            _fromObj = null;
                        }
                    }
                });
            }
            // Initialize draggable: Widget List to Column
            if (!$(draggableWidgetItem).hasClass("ui-draggable")) {
                $(draggableWidgetItem).draggable({
                    connectToSortable: sortableHolder,
                    helper: "clone"
                });
            }
        })();
        //);
    },
    //Save the widget data when we drag a new widget to some one column
    addWidget: function (fromObj, callBack) {
        $.get("AJAXFunc.aspx",
              { widgettypeid: fromObj.widgettypeid, index: fromObj.index, columnid: fromObj.columnid, pageid: pageId, method: "addWidget" },
              function (data) {
                  if (data) {
                      if (callBack != null) {
                          callBack();
                      }
                  }
              });
    },
    //Save the relative data when move widget between each two.
    moveWidget: function (fromObj, toObj, callBack) {
        $.get("AJAXFunc.aspx",
             {
                 widgettypeid: fromObj.widgettypeid,
                 index: fromObj.index,
                 columnid: fromObj.columnid,
                 towidgettypeid: toObj.widgettypeid,
                 toindex: toObj.index,
                 tocolumnid: toObj.columnid,
                 pageid: pageId,
                 method: "moveWidget"
             },
             function (data) {
                 if (data) {
                     if (callBack != null) {
                         callBack();
                     }
                 }
             });
    },
    //Create a New Widget Snap to mack when we add a new widget to the Column
    createNewWidgetSnap: function (widgetTitle) {
        return $('<div class="widgetContainer">'
                        + '<div class="widgetHeader">'
                        + '<span class="widgetTitle">&nbsp;</span>'
                        + '<div class="widgetHeaderButton">'
                        + '<a id="collapseWidgetAnchor" class="editwidget widget_collapse"></a>'
                        + '</div>'
                        + '</div>'
                        + '<div class="widgetHolder">'
                        + 'loading ...'
                        + '</div>'
                        + '<div class="widgetFooter">'
                        + '</div>'
                        + '</div>').find(".widgetTitle").html(widgetTitle).parents(".widgetContainer");
    }
}
 